package com.wandian.demo.other
{
	import com.greensock.*;
	import com.greensock.easing.*;
	import com.greensock.plugins.*;
	import com.wandian.demo.controller.LinesController;
	import com.wandian.demo.ui.LinesDisplayObject;

	import fl.transitions.PixelDissolve;
	import fl.transitions.Transition;
	import fl.transitions.TransitionManager;
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	import fl.transitions.easing.Regular;

	import flash.display.*;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.*;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.*;
	import flash.trace.Trace;
	import flash.utils.Timer;

	import utils.AnimationButton;
	import utils.PopUpManagerAS;
	import utils.Res;
	import utils.UIHelper;

	[SWF(backgroundColor="0xffffff", width="982", height="736")]
	public class TweenDemo extends Sprite
	{
		private var content:Sprite=new Sprite();

		public function TweenDemo()
		{
			super();
			trace("Tween Demo");
			initStage();
		}

		private function initStage():void
		{
			var loader:Loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onResLoadComplete);
			var context:LoaderContext=new LoaderContext(false, resDomain);
			loader.load(new URLRequest("res.swf"), context);
		}
		private var resDomain:ApplicationDomain=new ApplicationDomain(ApplicationDomain.currentDomain);

		private function onResLoadComplete(e:Event):void
		{
			//初始化玩资源文件，就开始初始化定义样式
			Res.init(resDomain);
			//UIHelper.addBitmapChild(this, "gamebg");
//		    testSelectLines();
			testPopUp();
		}

		private function testPopUp():void
		{
			// TODO Auto Generated method stub
			PopUpManagerAS.instance().stageRoot=this.stage;
			PopUpManagerAS.instance().isUserSty=true;
			var bitmap:Bitmap=new Bitmap(Res.get("spin_btn"));
			PopUpManagerAS.instance().addPopUp(this, bitmap,true);
			//PopUpManagerAS.instance().centerPopUp(bitmap);
		}

		private function testSelectLines():void
		{
			LinesController.Instance.showLinesContainer(this);
////			return;
//			for (var i:int=0; i <LinesController.Instance.allLinesArray.length; i++)
//			{
//				UIHelper.addSpriteChild(this, new LinesDisplayObject("line" + LinesController.Instance.allLinesArray[i]),null,LinesController.Instance.allLinesX[i],LinesController.Instance.allLinesY[i],false,true);
			//}
			//LinesController.Instance.showLinesContainer(this);
			UIHelper.addBAnimationButtonChild(this, "selectline", onselectlineBtnClikHandler, 275, this.stage.stageHeight - 130, -5, -5);
		}

		private function onselectlineBtnClikHandler(event:MouseEvent):void
		{
			LinesController.Instance.doSelectLines();
		}

		private function testLines():void
		{
			LinesController.Instance.showLinesContainer(this);
			var timer:Timer=new Timer(1000, 30);
			timer.addEventListener(TimerEvent.TIMER, onTimerHandler);
			timer.start();
			function onTimerHandler(event:TimerEvent):void
			{
				LinesController.Instance.setLinesVisibleByIndex(timer.currentCount - 2, false);
				LinesController.Instance.setLinesVisibleByIndex(timer.currentCount - 1, true);
			}
		}

		private function testGetPix():void
		{
			var btn_spin:AnimationButton=UIHelper.addBAnimationButtonChild(this, "spin", onSpinBtnClikHandler, 360, stage.stageHeight - 130, -5, -5);
			function onSpinBtnClikHandler(event:MouseEvent):void
			{

			}

		}

		private function testMyTween():void
		{
			// TODO Auto Generated method stub
			//			var downBitMapData:BitmapData=new apple as BitmapData;		
			//			var btnDowMap:Bitmap=new Bitmap(downBitMapData);
			//			addChild(btnDowMap);
			//			TweenLite.to(btnDowMap, 1, {x:65, y:117});

			//testShapeMove();
			//testTextContent();
			//testShapeMove();
			//			testEasing();
			var ball:Sprite;
			ball=UIHelper.addRectSpriteChild(this, ball, "maskLayer", 0, 0, 100, 100, 0x2B2B2B, 1);
			var twTween:Tween=new Tween(ball, "y", Strong.easeOut, 0, 100, 3, true);
			twTween.addEventListener(TweenEvent.MOTION_FINISH, tweenFinishHandler);
			//var tmManager:TransitionManager=new TransitionManager(ball);
			//twTween.start();
			//twTween.stop();
			stage.addEventListener(MouseEvent.CLICK, stopTweenHandler);

			//			tmManager.startTransition(
			//				{type:PixelDissolve,
			//					direction:Transition.IN,
			//					duration:2,easing:Regular.easeOut,xSections:10,ySections:10});
			function tweenFinishHandler(Event:TweenEvent):void
			{
				trace("ok");
				//twTween.yoyo();
			}

			function stopTweenHandler(event:MouseEvent):void
			{
				twTween.start();
			}
		}

		private function testEasing():void
		{
			// TODO Auto Generated method stub
			var maskLayer:Sprite;
			maskLayer=UIHelper.addRectSpriteChild(this, maskLayer, "maskLayer", 0, 0, 100, 100, 0x2B2B2B, 1);
			TweenLite.to(maskLayer, 3, {y: 100});
			//TweenLite.from(maskLayer,5, {alpha: 1, y: 0});
			//TweenLite.to(maskLayer,10,{x:300,y:300,rotation:360});			
//			Tweener.addTween(this,{Acceleration:70,time:0.4, transition:"easeInSine",onComplete:SoundPlay});//70是控制旋转速度			
		}

		private function testTextContent():void
		{
			// TODO Auto Generated method stub

			content.x=content.y=18;
			content.rotation=-6;

			var format:TextFormat=new TextFormat();
			format.font="Times New Roman";
			format.size=15;
			format.leading=2;

			addChild(content);
			var text:String="Blitting can significantly improve scrolling performance for large areas of text and/or images. To see the difference compared to normal mask and scrollRect techniques, select from the options below and click the TWEEN button. Then watch the frame rate. Blitting typically uses more memory but requires much less processing while scrolling/moving. BlitMask basically captures a bitmap version of the masked content and stores it in memory and then draws only the necessary pixels into the mask area and sets the 'visible' property of the original content to false, thus alleviating the Flash Player from having to worry about all those extra pixels. Maximum frame rate in this example is 60fps. For maximum performance, enable BlitMask's bitmapMode (like during animation) and then when you need interactivity, disable it. BlitMask even outperforms scrollRect with cacheAsBitmap set to true, and it also allows the content to be rotated, scaled, and smoothed (try that with scrollRect!). BlitMask is much easier to use too.";
			for (var i:int=0; i < 14; i++)
			{
				var tf:TextField=new TextField();
				tf.text=text + "\n" + text + "\n" + text;
				tf.multiline=tf.embedFonts=tf.wordWrap=true;
				tf.antiAliasType="normal";
				tf.selectable=false;
				tf.textColor=0xFFFFFF;
				tf.width=370;
				tf.height=1000;
				tf.setTextFormat(format);

				content.addChild(tf);
				tf.x=tf.width * (i - 2) + 5;
			}
			var bm:BlitMask=new BlitMask(content, content.x, content.y, 514, 350, false);
			TweenLite.to(content, 30, {x: -3000, onUpdate: bm.update});
		}

		private function testShapeMove():void
		{
			// TODO Auto Generated method stub

			var shape:Sprite=new Sprite;
			shape.name="shape1";
			shape.graphics.beginFill(0x344523);
			shape.graphics.drawRect(0, 0, 100, 100);
			shape.graphics.endFill();
			addChild(shape);
			//TweenLite.to(shape, 10, {alpha: 1, x: 100, y: 100, ease: Bounce.easeIn});

			TweenLite.to(shape, 5, {x: 0, y: 100, onComplete: onComplete, ease: Bounce.easeOut});
			function onComplete():void
			{
				trace("tween flash complete");
			}
		}

	}
}
